package com.book.bookshop.web;

import com.book.bookshop.entity.BsUser;
import com.book.bookshop.service.BsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private BsUserService bsUserService;

    //验证用户名是否存在
    @RequestMapping("/checkUserName")
    @ResponseBody //转成json否则会在页面中去找
    public String checkUserName(String username) {
        return bsUserService.checkUser(username);
    }

    @ResponseBody
    @RequestMapping("/register")
    public String register(BsUser bsUser){
        boolean save = bsUserService.save(bsUser);
        if(save){
            return "success";
        }else {
            return "fail";
        }
    }

    @ResponseBody
    @RequestMapping("/login")
    public String login(BsUser bsUser, HttpSession httpSession){
        return bsUserService.login(bsUser,httpSession);
    }

    @RequestMapping("/logout")
    public String logout(HttpSession httpSession){
        httpSession.invalidate();
        return "redirect:/book/index";//重定向
    }


}
